<?php
class main extends spController
{
	function __construct(){ // 公用
		parent::__construct(); // 这是必须的
		$this->tpl_title = "产品模块|主题部分";
	}
	
	function index(){ // 这里是首页
		$this->display("produce/main_index.html");
	}
	
	function page(){ // 其他内容
		echo '<h2>'.'检测功能'.$i.'</h2>';
		
		$this->start_detection();
		$this->contents = "这里是检测木块";
		$this->display("produce/main_page.html");
	}
	
	function start_detection()
	{
		//这里是开启检测功能
		//$this->detection();
		$time = $this->spArgs('time');
		$this->moniter($time);
	}
	
	function stop_detection()
	{
		//这里是停止检测功能
	}
	
      //检测
	public function detection($interval)
	{		
		$site = spClass("site");
		$where['active'] = 1;
		$tttc=60;
		$where['intervals'] = $interval / $tttc;
		$b = $site->findAll($where);
		 dump($b);
		foreach ($b as $i)
		{
			$runtime = spClass("runtime");
 	        $dat_time=date("Y-m-d H:i:s");
 	        $runtime->query("update baomu_runtime set runtime='$dat_time' where time_id=1");       

            $url = 'http://'.$i[domain];
            $s_date = date("Y-m-d H:i:s");
            $status=$this->http_p($url);
            $n_date = date("Y-m-d H:i:s");
            $sb=strtotime($n_date)-strtotime($s_date);
          
            import("detection.php");
            $get_ip = new detection();
			$ip=$get_ip->g_ip($i[domain]);
          
			$Model = spClass('record');
			$data = array
			(
				'status_code'=>$status,
				'userid'=>$i[userid],
				'siteid'=>$i[siteid],
				'type'=>'',
				'size'=>'0',
				'taketime'=>$sb,
				'addtime'=>strtotime(date("Y-m-d H:i:s")),
				'site_ip'=>$ip,
			); 	
			$Model->create($data);

			$site->create();
			$ssdate['status_code']=$status;
			$ssdate['lasttime']=$data['addtime'];
			$wwe['siteid']=$i[siteid];
			$site->update($wwe,$ssdate);
		}
      	
	}
	
	
 /**
    +----------------------------------------------------------
    * 网站链接测试
    +----------------------------------------------------------
    */
    public function http_p($url,$method,$postDate)
    {
        $method ='GET';
        $postDate = '';
        import("WebRequest.php");
        $ht_tp = new WebRequest($url,$method,$postDate);
        
        if (!$c=$ht_tp->getResponse())
        {
        	return 'could not open';
        }
        else 
        {        	
        	return $c->status;
        	$this->display("produce/main_page.html");
        }
    }
    
	function start_ping()
	{
		//这里是ping功能
	}
	

    /**
    +----------------------------------------------------------
    * 循环
    +----------------------------------------------------------
    */
    
	private function moniter($time)
    {    	
    	ignore_user_abort();
        set_time_limit(0);
        $interval= $time;
        
        do
        {        	
			$this->detection($interval);
			sleep($interval);

		}while(true);
    
      }
}